#include "stdafx.h"
#include "StrUtil.h"

#include "UIDefaultValue.h"

namespace StrUtil
{
    ExportXSkinLib BOOL    NextToken(const XString& strText, size_t& handle, LPCTSTR szSep, XString& strToken)
    {
        return NextToken(strText, handle, szSep, strToken, FALSE);
    }

    ExportXSkinLib BOOL NextToken(const XString& strText, size_t& handle, LPCTSTR szSep, XString& strToken, BOOL bAllowEmptyToken)
    {
        strToken = _T("");
        if(handle >= strText.size())
        {
            if(strText.empty() && handle == 0 && bAllowEmptyToken)
            {
                ++ handle;
                return TRUE;
            }
            return FALSE;
        }

        XString strSep(szSep);

        do
        {
            size_t nPos = strText.find(strSep, handle);
            if(nPos == XString::npos)
                strToken = strText.substr(handle);
            else if(nPos == handle)
                strToken = _T("");
            else
                strToken = strText.substr(handle, nPos - handle);

            if(nPos == XString::npos)
                handle += strToken.size();
            else
                handle = nPos + strSep.size();

        } while(handle < strText.size() && (!bAllowEmptyToken && strToken.empty()));

        return (!strToken.empty() || bAllowEmptyToken);
    }

    ExportXSkinLib XString RemoveToken(LPCTSTR szText, LPCTSTR szStartTag, LPCTSTR szEndTag)
    {
        XString strResult(szText);

        size_t nStartTagLen = _tcslen(szStartTag);
        size_t nEndTagLen = _tcslen(szEndTag);

        size_t nStartPos = 0;
        size_t nEndPos = 0;

        nStartPos = strResult.find(szStartTag);
        while( nStartPos != XString::npos)
        {
            nEndPos = strResult.find(szEndTag, nStartPos + nStartTagLen);
            if(nEndPos == XString::npos)
                break;

            strResult.erase(nStartPos, nEndPos + nEndTagLen - nStartPos);
            nStartPos = strResult.find(szStartTag, nStartPos);
        }
        return strResult;
    }

    ExportXSkinLib size_t CalcProperTextLen(IN XCANVAS xCanvas, IN const XString& strText, IN OUT RECT& rcText, IN int nLineWidth, IN DWORD dwDrawFlags, IN HFONT hFont)
    {
        if(nLineWidth <= 0)
            return XString::npos;

        size_t nTextLen = strText.size();
        LPCTSTR szText = strText.GetData();
        if(strText.empty())
        {
            nTextLen = XDEMO_SINGLINE_TEXT_LENGTH;
            szText = XDEMO_SINGLINE_TEXT;
        }

        if((dwDrawFlags & DT_RIGHT) == DT_RIGHT)
        {
            rcText.right = rcText.left + nLineWidth;
            return strText.size();
        }

        rcText.right = rcText.left + nLineWidth;
        dwDrawFlags = dwDrawFlags & (DT_SINGLELINE | DT_EXPANDTABS | DT_NOPREFIX | DT_WORDBREAK);
        dwDrawFlags |= (DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE | DT_WORDBREAK);

        HGDIOBJ hOldFont = NULL;
        if(hFont != NULL)
            hOldFont = ::SelectObject((HDC)xCanvas, hFont);

        size_t nResult = nTextLen + 1;
        do 
        {
            -- nResult;
            ::DrawText((HDC)xCanvas, szText, nResult, &rcText, dwDrawFlags);
        } while (rcText.right - rcText.left > nLineWidth && nResult > 0);

        if(nResult == 0)
            nResult = -1;

        if(strText.empty())
            rcText.right = rcText.left;

        if(nResult > 0 && rcText.right > rcText.left)
        {
            TEXTMETRIC metric;
            if(::GetTextMetrics((HDC)xCanvas, &metric) && metric.tmItalic)
            {
                rcText.right += metric.tmAveCharWidth / 2;
            }
        }

        if(hFont != NULL)
            ::SelectObject((HDC)xCanvas, hOldFont);
        return nResult;
    }


}